<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-dircache.html" />
<link rel="prev" href="module-linecache.html" />
<link rel="parent" href="filesys.html" />
<link rel="next" href="shutil-example.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>11.10 shutil -- High-level file operations</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="11.9 linecache  "
  href="module-linecache.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="11. file and Directory"
  href="filesys.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="11.10.1 Example"
  href="shutil-example.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-linecache.html">11.9 linecache  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="filesys.html">11. File and Directory</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="shutil-example.html">11.10.1 Example</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION00131000000000000000000">
11.10 <tt class="module">shutil</tt> --
         High-level file operations</a>
</h1>

<p>
<a name="module-shutil"></a>

<p>
The <tt class="module">shutil</tt> module offers a number of high-level operations on
files and collections of files.  In particular, functions are provided 
which support file copying and removal.
<a id='l2h-2310' xml:id='l2h-2310'></a>

<p>
<strong>Caveat:</strong>  On MacOS, the resource fork and other metadata are
not used.  For file copies, this means that resources will be lost and 
file type and creator codes will not be correct.

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2300' xml:id='l2h-2300' class="function">copyfile</tt></b>(</nobr></td>
  <td><var>src, dst</var>)</td></tr></table></dt>
<dd>
  Copy the contents of the file named <var>src</var> to a file named
  <var>dst</var>.  The destination location must be writable; otherwise, 
  an <tt class="exception">IOError</tt> exception will be raised.
  If <var>dst</var> already exists, it will be replaced.  
  Special files such as character or block devices
  and pipes cannot be copied with this function.  <var>src</var> and
  <var>dst</var> are path names given as strings.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2301' xml:id='l2h-2301' class="function">copyfileobj</tt></b>(</nobr></td>
  <td><var>fsrc, fdst</var><big>[</big><var>, length</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Copy the contents of the file-like object <var>fsrc</var> to the
  file-like object <var>fdst</var>.  The integer <var>length</var>, if given,
  is the buffer size. In particular, a negative <var>length</var> value
  means to copy the data without looping over the source data in
  chunks; by default the data is read in chunks to avoid uncontrolled
  memory consumption. Note that if the current file position of the
  <var>fsrc</var> object is not 0, only the contents from the current file
  position to the end of the file will be copied.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2302' xml:id='l2h-2302' class="function">copymode</tt></b>(</nobr></td>
  <td><var>src, dst</var>)</td></tr></table></dt>
<dd>
  Copy the permission bits from <var>src</var> to <var>dst</var>.  The file
  contents, owner, and group are unaffected.  <var>src</var> and <var>dst</var>
  are path names given as strings.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2303' xml:id='l2h-2303' class="function">copystat</tt></b>(</nobr></td>
  <td><var>src, dst</var>)</td></tr></table></dt>
<dd>
  Copy the permission bits, last access time, and last modification
  time from <var>src</var> to <var>dst</var>.  The file contents, owner, and
  group are unaffected.  <var>src</var> and <var>dst</var> are path names given
  as strings.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2304' xml:id='l2h-2304' class="function">copy</tt></b>(</nobr></td>
  <td><var>src, dst</var>)</td></tr></table></dt>
<dd>
  Copy the file <var>src</var> to the file or directory <var>dst</var>.  If
  <var>dst</var> is a directory, a file with the same basename as <var>src</var> 
  is created (or overwritten) in the directory specified.  Permission
  bits are copied.  <var>src</var> and <var>dst</var> are path names given as
  strings.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2305' xml:id='l2h-2305' class="function">copy2</tt></b>(</nobr></td>
  <td><var>src, dst</var>)</td></tr></table></dt>
<dd>
  Similar to <tt class="function">copy()</tt>, but last access time and last
  modification time are copied as well.  This is similar to the
  <span class="Unix">Unix</span> command <b class="program">cp</b> <b class="programopt">-p</b>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2306' xml:id='l2h-2306' class="function">copytree</tt></b>(</nobr></td>
  <td><var>src, dst</var><big>[</big><var>, symlinks</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Recursively copy an entire directory tree rooted at <var>src</var>.  The
  destination directory, named by <var>dst</var>, must not already exist;
  it will be created as well as missing parent directories.
  Permissions and times of directories are copied with <tt class="function">copystat()</tt>,
  individual files are copied using <tt class="function">copy2()</tt>.  
  If <var>symlinks</var> is true, symbolic links in
  the source tree are represented as symbolic links in the new tree;
  if false or omitted, the contents of the linked files are copied to
  the new tree.  If exception(s) occur, an <tt class="exception">Error</tt> is raised
  with a list of reasons.

<p>
The source code for this should be considered an example rather than 
  a tool.

<p>

<span class="versionnote">Changed in version 2.3:
<tt class="exception">Error</tt> is raised if any exceptions occur during
                  copying, rather than printing a message.</span>

<p>

<span class="versionnote">Changed in version 2.5:
Create intermediate directories needed to create <var>dst</var>,
                  rather than raising an error. Copy permissions and times of
		  directories using <tt class="function">copystat()</tt>.</span>

<p>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2307' xml:id='l2h-2307' class="function">rmtree</tt></b>(</nobr></td>
  <td><var>path</var><big>[</big><var>, ignore_errors</var><big>[</big><var>, onerror</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Delete an entire directory tree.<a id='l2h-2311' xml:id='l2h-2311'></a>
  If <var>ignore_errors</var> is true,
  errors resulting from failed removals will be ignored; if false or
  omitted, such errors are handled by calling a handler specified by
  <var>onerror</var> or, if that is omitted, they raise an exception.

<p>
If <var>onerror</var> is provided, it must be a callable that accepts
  three parameters: <var>function</var>, <var>path</var>, and <var>excinfo</var>.
  The first parameter, <var>function</var>, is the function which raised
  the exception; it will be <tt class="function">os.listdir()</tt>, <tt class="function">os.remove()</tt> or
  <tt class="function">os.rmdir()</tt>.  The second parameter, <var>path</var>, will be
  the path name passed to <var>function</var>.  The third parameter,
  <var>excinfo</var>, will be the exception information return by
  <tt class="function">sys.exc_info()</tt>.  Exceptions raised by <var>onerror</var> will
  not be caught.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2308' xml:id='l2h-2308' class="function">move</tt></b>(</nobr></td>
  <td><var>src, dst</var>)</td></tr></table></dt>
<dd>
Recursively move a file or directory to another location.

<p>
If the destination is on our current filesystem, then simply use
rename.  Otherwise, copy src to the dst and then remove src.

<p>

<span class="versionnote">New in version 2.3.</span>

</dl>

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-2309' xml:id='l2h-2309' class="exception">Error</tt></b></dt>
<dd>
This exception collects exceptions that raised during a mult-file
operation. For <tt class="function">copytree</tt>, the exception argument is a
list of 3-tuples (<var>srcname</var>, <var>dstname</var>, <var>exception</var>).

<p>

<span class="versionnote">New in version 2.3.</span>

</dd></dl>

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="shutil-example.html">11.10.1 Example</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="11.9 linecache  "
  href="module-linecache.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="11. file and Directory"
  href="filesys.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="11.10.1 Example"
  href="shutil-example.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-linecache.html">11.9 linecache  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="filesys.html">11. File and Directory</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="shutil-example.html">11.10.1 Example</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
